<?php
/*
Template Name: modules
*/
?>
<?php //require( dirname(__FILE__) . '/wp-load.php' ); 
setlocale(LC_ALL,'es_ES');
?>
<link href="/css/generales.css" rel="stylesheet" type="text/css"/>
<link href="http://www.elespectador.com/sites/all/themes/garland/css/s3/home.css" rel="stylesheet" type="text/css"/>
<link href="http://blogs.elespectador.com/wp-content/themes/elespectador/css/blogs.css" rel="stylesheet" type="text/css"/>




<?php 
//version 
    global $wpdb;
	global $table_prefix;
    global $word_limit;
    global $title_begin_wrap;
    global $title_end_wrap;
    global $begin_wrap;
    global $end_wrap;
    global $read_more;
    global $show_author;
    global $show_blog;
    
	$blog_list = $wpdb->get_results( "SELECT blog_id from wp_blogs where public = 1" );
    
	$intArrayCount = 0;
    $bid = '';
	
	$sqlGetPosts = ""; //print_r($arrayName);
	foreach ($blog_list as $arrayElement) {
		$bid = $arrayElement->blog_id;
		if ($bid!=1){
			$tmp_SQL = "SELECT post_content, post_title, post_author, post_modified_gmt, post_date_gmt, $bid as blog_id,guid  FROM wp_".$bid."_posts WHERE post_type = 'post' and post_status= 'publish '";
			
			if ( strlen( $sqlGetPosts) > 0){
				$sqlGetPosts .= "UNION ";
			}			
			$sqlGetPosts .= $tmp_SQL;
		}
	}
	
	
	$sqlGetPosts2=$sqlGetPosts; 
	$sqlGetPosts .= " ORDER BY post_modified_gmt DESC limit 7"; 
	$post_list = $wpdb->get_results( $sqlGetPosts, ARRAY_A );
	$post_list2 = $wpdb->get_results( $sqlGetPosts2, ARRAY_A );
	
	//print_r($post_list2); 
	$j=0;
	foreach ($post_list as $post) { 
		$content_post=$post["post_content"];
		$content_post=explode('<!--more-->',$content_post); 
		$title_post = $post["post_title"];
		
		
		$tmp_post_content = $post["post_content"];
		$tmp_post_content = list_all_posts_summary_multi($tmp_post_content, 80);
		$tmp_post_title = $post["post_title"];
		$tmp_post_fecha = $post["post_modified_gmt"];
		$fecha_post  = explode(":", $tmp_post_fecha);
     	$varaux = explode("-", $fecha_post[0]);
		$varano = $varaux[0];
//		echo $varano;
		$varmes = $varaux[1];
//		echo $varmes;
		$vardia = $varaux[2];

		$vardi= explode(" ", $vardia);

		
		$id_author = get_author_blog($post["blog_id"]); 
		$author="";
		$datos=get_author_name2($id_author); 
		$firstname=$datos['firstname']; $lastname=$datos['lastname'];
			if (($datos['firstname'] =! ' ') || ($datos['lastname'] =! ' '))
				{
					$author= $firstname." ".$lastname;
				}else{$author=$datos['nickname'];}
		
		$section=get_posts_section($post["blog_id"]); 

		$tmp_post_author = $wpdb->get_var("SELECT display_name FROM $wpdb->users WHERE ID = '" . $post["post_author"] . "'");
		
		$bid = $post["blog_id"];
 
		$tmp_blog_name = $wpdb->get_var("SELECT option_value FROM " . "wp_" . $bid . "_options" . " WHERE option_name = 'blogname'");
		
		$tmp_domain = $wpdb->get_var("SELECT domain FROM wp_blogs WHERE blog_id = '" . $bid . "'");
		
		$tmp_path = $wpdb->get_var("SELECT path FROM wp_blogs WHERE blog_id = '". $bid."'");


		$tmp_siteurl = $wpdb->get_var("SELECT option_value FROM " . "wp_" . $bid . "_options" . " WHERE option_name = 'siteurl'");
//		echo $tmp_siteurl;
		$info = ' - Posted ';
		if ($show_blog == "show"){
			$info = $info .'to ' . $tmp_blog_name . ' ';				
		}
		if ($show_author == "show"){
			$info = $info .'by ' . ucfirst($tmp_post_author);				
		}
		if ($show_author && $show_blog == 'hide'){
			$info = '';				
		}
		
		$info = stripslashes($info);
	
		$img = get_string_between($tmp_post_content, '<img','/>');
		
		//echo $img;
		$img_tam = get_string_between($tmp_post_content, '-','jpg');
		$img_tamano = get_string_between($tmp_post_content, 'src="http','jpg');
		$img_tamano = get_string_between($img_tamano, '-','.');
		$leyenda=get_string_between($img,'alt="','"'); 
		
		$img_width= 'width="'.($img_width = get_string_between($img, 'width="','"'));
	    $img_height= 'height="'.($img_height = get_string_between($img, 'height="','"'));
		$img = str_replace($img_width,'width="174', $img);
		$img = str_replace('height="320','height="116', $img);


		$audio = get_string_between($tmp_post_content, '.mp3','</a>');
		$audio2 = get_string_between($tmp_post_content, '[audio',']');

		$gale = get_string_between($tmp_post_content, '[galler',']');
//		print_r($gale);

		

		$video = get_string_between($tmp_post_content, '[youtube]','[/youtube]');
		//echo $video;
		if($video!='')
		{
			$video = substr($video,strpos($video,'v=')+2);
		}
		

		$caption = get_string_between($tmp_post_content, '[caption id','[/caption]');


		
		//print_r($caption);

		$tmp_post_content = str_replace('<img'.$img.'/>','', $tmp_post_content);
		$tmp_post_content = str_replace('[youtube]'.$video.'[/youtube]','', $tmp_post_content);
		$tmp_post_content = str_replace('[caption id'.$caption.'[/caption]','', $tmp_post_content);	
		$tmp_post_content = str_replace('[galler'.$gale.']','', $tmp_post_content);
		$tmp_post_content = str_replace('[audio'.$audio2.']','', $tmp_post_content);

		$caption =''; $imagen = ''; $youtube =''; $galler =''; $audio ==''; $leyenda =='';
					$caption = get_string_between($content_post[0],'[caption id','[/caption]');
																				   
					$imagen = get_string_between($content_post[0],'<img','/>');
					$youtube = get_string_between($content_post[0],'[youtube]','[/youtube]');
					$galler = get_string_between($content_post[0],'[galler',']');
					$audio = get_string_between($content_post[0],'[audio',']');
					$praro = get_string_between($content_post[0],'<p','</p>');
					
					$info=$content_post[0];
					$info= str_replace('[caption id'.$caption.'[/caption]','',$info);
					$info= str_replace('<p'.$praro.'</p>','',$info);
					$info= str_replace('<img'.$imagen.'/>','',$info);																		
					$info= str_replace('[youtube]'.$youtube.'[/youtube]','',$info);
					$info= str_replace('[galler'.$galler.']','',$info);																		
					$info= str_replace('[audio'.$audio.']','',$info);			
				 
				 	$img_width= get_string_between($imagen, 'width="','"');
	    			$img_height= get_string_between($imagen, 'height="','"');
					
					if($img_width > 174){ 
						$imagen = str_replace('width="'.$img_width .'"','width="174"', $imagen);
					}
					if($img_height > 116){
						$imagen = str_replace('height="'.$img_height.'"','height="116"', $imagen);
					}
				 
					$cap_width= get_string_between($caption, 'width="','"');
	    			$cap_height= get_string_between($caption, 'height="','"');
					
					if($cap_width > 174){
						$caption = str_replace('width="'.$cap_width.'"','width="174"', $caption);
					}
					if($cap_height > 116){
					$caption = str_replace('height="'.$cap_height.'"','height="116"', $caption);}
					
					$caption=str_replace('class="recurso_post','class="', $caption);
					
					
					
				if ($caption!= ''){
						$leyenda=get_string_between($caption,'alt="','"');
						$basurita=get_string_between($caption,'=',']');
						$caption=str_replace('='.$basurita.']','',$caption);
						//echo '<div class="recurso">'.$caption.'<h3>'.$leyenda.'</h3></div>';		
					}
					elseif($imagen != ''){//echo'<img'.$imagen.'/>'; 
					}
					
				
		if ($j < 3){
			 
		if($j==2){$estilo='style="border: medium none ; "';}else{$estilo='';}
		if($j==0){echo '<div class="blogs_destacados">';}
		?>
			<div class="un_blog_destacado" <?php echo $estilo;?> >
				<div class="titulo_blog">
					
					<?php
						
						if ($section[0]->id_section==''){}else{						
						echo '<span><a href="http://blogs.elespectador.com/seccion?id='.$section[0]->id_section.'"><img src="http://blogs.elespectador.com/images/seccion_'.$section[0]->image.'.gif" border="0"/></a></span>';}
					?>
                    <h1><a<?php echo " href='". $tmp_siteurl ."'"; ?>><?php echo substr($tmp_blog_name,0,28); ?></a></h1>
                    <h2><a href="<?php echo $tmp_siteurl.'autor'; ?>">Por: <?php echo $tmp_post_author;?></a></h2>
					<div class="clear"></div>
				</div>
                <a href="<?php echo $tmp_siteurl.'autor'; ?>">
				<?php 	
				
               if ($id_author=='' || $id_author==1){
						echo '<img src="http://blogs.elespectador.com/wp-content/themes/elespectador/images/avatar_default_blogs_ee.jpg" height="70" width="70" style="float: left;"/>';
					}else{
						
						echo userphoto__get_userphoto2($id_author,3,'','',array('style' => 'width:70px; height:70px; float: left;'),'http://blogs.elespectador.com/wp-content/themes/elespectador/images/avatar_default_blogs_ee.jpg',$tmp_siteurl."files/");
					} ?>
                  </a>
				<div class="info_blog">
					<h2><a href="<?php echo $post['guid']; ?>"><?php echo $title_post;?></a><?  echo strftime("%e %B %Y",strtotime($varmes."/".$vardi[0]."/".$varano))?></h2>
					
				<?php //echo "<p><a href='". $tmp_siteurl ."'>".substr($info,0,150)." ... </a></p>";
				//echo list_all_posts_summary_multi($tmp_post_content, $word_limit); ?>
				<div class="clear"></div>
                </div>
			</div>
		<?php 
			if($j==2){echo '</div>';}
			
        	
        }else{
        	
		if ($j==3){echo '<div class="otros_blogs"><div class="ultimas_entradas">';}
		?><div class="una_entrada">
   			<h2><a<?php echo " href='". $tmp_siteurl ."'"; ?>><?php echo $tmp_blog_name; ?></a></h2>
            <h4><a href="<?php echo $tmp_siteurl.'autor'; ?>">Por: <?php echo $tmp_post_author;?></a></h4>
            <div class="clear"></div>
   			<h3><a href="<?php echo $post['guid']; ?>"><?php echo $title_post;?></a></h3>

			
			<div class="clear"></div>
			<h5><?php echo strftime("%e %B %Y",strtotime($varmes."/".$vardi[0]."/".$varano)); ?></h5>
		</div>
		<?php
		if ($j==6){echo '<div class="todos_blogs"><a href="http://blogswp.iccknet.com"><img alt="" src="http://static.elespectador.com/blogs/todos.gif" style="float: right;"/></a><div class="clear"/></div></div></div><div class="blogs_publicidad"><a href="http://blogswp.iccknet.com"><img src="http://static.elespectador.com/images/banner_promo.jpg" /></a></div></div>';}
		
        
        } $j++;

	}

	

?>